package com.kikoulol.jarmoryextractor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.kikoulol.jarmoryextractor.utils.Utils;

public class LadderPage {

	Element ladderXml;
	String armoryUrl;
	
	private String globalSearch; 
	private String lang; 
	private String requestQuery; 
	private String requestUrl;  
	private String battleGroup;
	private String filterField;
	private String filterValue ;
	private String level;
	private String maxPage;
	private String page;
	private String sortDir; 
	private String sortField;
	private String teamSize;
	private String url; 
	private List<ArenaTeam> arenaTeams = new ArrayList<ArenaTeam>();
	
	public LadderPage(String url) throws IOException, ParserConfigurationException, SAXException {
		String xml = Utils.getArmoryXml(url);
		armoryUrl = "http://" + url.split("/")[2] + "/";
		Element ladderXml;
		try {
			ladderXml = Utils.getXmlRootElement(xml);
		}
		catch (Exception e) {
			File fileToRemove = new File(Utils.getLocalUrl(url));
			fileToRemove.delete();
			xml = Utils.getArmoryXml(url);
			ladderXml = Utils.getXmlRootElement(xml);
		}
		init(ladderXml);
	}

	private void init(Element ladderXml) {
		
		this.ladderXml = ladderXml;
		
		// Param globale de la Page
		setGlobalSearch(ladderXml.getAttributeNode("globalSearch").getValue());
		setLang(ladderXml.getAttributeNode("lang").getValue());
		setRequestQuery(ladderXml.getAttributeNode("requestQuery").getValue());
		setRequestUrl(ladderXml.getAttributeNode("requestUrl").getValue());
		
		// Param globale de l'arenaLadderPageResult
		Node arenaLadderPagedResult = ladderXml.getElementsByTagName("arenaLadderPagedResult").item(0);
		setBattleGroup(arenaLadderPagedResult.getAttributes().getNamedItem("battleGroup").getNodeValue());
		setFilterField(arenaLadderPagedResult.getAttributes().getNamedItem("filterField").getNodeValue());
		setFilterValue(arenaLadderPagedResult.getAttributes().getNamedItem("filterValue").getNodeValue());
		setLevel(arenaLadderPagedResult.getAttributes().getNamedItem("level").getNodeValue());
		setMaxPage(arenaLadderPagedResult.getAttributes().getNamedItem("maxPage").getNodeValue());
		setPage(arenaLadderPagedResult.getAttributes().getNamedItem("page").getNodeValue());
		setSortDir(arenaLadderPagedResult.getAttributes().getNamedItem("sortDir").getNodeValue());
		setSortField(arenaLadderPagedResult.getAttributes().getNamedItem("sortField").getNodeValue());
		setTeamSize(arenaLadderPagedResult.getAttributes().getNamedItem("teamSize").getNodeValue());
		setUrl(arenaLadderPagedResult.getAttributes().getNamedItem("url").getNodeValue());
	}

	public String getGlobalSearch() {
		return globalSearch;
	}

	public void setGlobalSearch(String globalSearch) {
		this.globalSearch = globalSearch;
	}

	public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

	public String getRequestQuery() {
		return requestQuery;
	}

	public void setRequestQuery(String requestQuery) {
		this.requestQuery = requestQuery;
	}

	public String getRequestUrl() {
		return requestUrl;
	}

	public void setRequestUrl(String requestUrl) {
		this.requestUrl = requestUrl;
	}

	public String getBattleGroup() {
		return battleGroup;
	}

	public void setBattleGroup(String battleGroup) {
		this.battleGroup = battleGroup;
	}

	public String getFilterField() {
		return filterField;
	}

	public void setFilterField(String filterField) {
		this.filterField = filterField;
	}

	public String getFilterValue() {
		return filterValue;
	}

	public void setFilterValue(String filterValue) {
		this.filterValue = filterValue;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(String maxPage) {
		this.maxPage = maxPage;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getSortDir() {
		return sortDir;
	}

	public void setSortDir(String sortDir) {
		this.sortDir = sortDir;
	}

	public String getSortField() {
		return sortField;
	}

	public void setSortField(String sortField) {
		this.sortField = sortField;
	}

	public String getTeamSize() {
		return teamSize;
	}

	public void setTeamSize(String teamSize) {
		this.teamSize = teamSize;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public List<ArenaTeam> getArenaTeams() throws IOException, ParserConfigurationException, SAXException {
		NodeList teamsXml = ladderXml.getElementsByTagName("arenaTeam");
		for (int i = 0; i < teamsXml.getLength(); i++) {
			Node team = teamsXml.item(i);
			String teamUrl = team.getAttributes().getNamedItem("realmUrl").getNodeValue();
			ArenaTeam arenaTeam = new ArenaTeam(armoryUrl +  "team-info.xml?" + teamUrl);
			arenaTeams.add(arenaTeam);
		}
		return arenaTeams;
	}
}
